﻿using MangoMes.Commont.Domain.RBACs;
using MangoMes.Commont.DTOs;
using MediatR;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using RBAC.Write.API.Applications.PermissionsFiles;
using RBAC.Write.Domain.DTOs;

namespace RBAC.Write.API.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class RBACController : ControllerBase
    {
        private readonly IMediator mediator;

        public RBACController(IMediator mediator)
        {
            this.mediator = mediator;
        }

        /// <summary>
        /// 权限添加
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ResultsDTO> CreatePermission(Createpercommand info)
        {
            try
            {
                var cou = await mediator.Send(info);
                return cou;
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 权限列表
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<ResultsDTO<Permissions>> getPermissionList()
        {
            try
            {
                var list=await mediator.Send(new PermissisonListCommand());
                return list;
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 角色添加
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<ResultsDTO> craeteRoles(RolesCraeteCommand info)
        {
            try
            {
                var list = await mediator.Send(info);
                return list;
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 角色列表
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<ResultsDTO<RolesListDTO>> ListRoles()
        {
            try
            {
                var list = await mediator.Send(new RolesListCommand());
                return list;
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 用户添加
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ResultsDTO> CreateUser(UserCreateCommand info)
        {
            try
            {
                var list = await mediator.Send(info);
                return list;
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 用户列表查询
        /// </summary>
        /// <param name="UserName">用户名</param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ResultsDTO<UserListDTO>> ListUser(string? UserName)
        {
            try
            {
                var list = await mediator.Send(new UserListCommand() { UserName= UserName });
                return list;
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 用户登录
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ResultsDTO> Login(LoginCommand info)
        {
            try
            {
                var list = await mediator.Send(info);
                return list;
            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}
